package ywh.manager
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import ywh.event.CoreEvent;
	import ywh.pool.ListPool;
	import ywh.pool.PlayerPool;
	import ywh.pool.StreamPool;
	
	/**
	 * 单例类:课程核心类;
	 * @author 杨伟华
	 * @version 1.0
	 *
	 * Created 1/22/2013 5:20 AM
	 */
	public class Core
	{
		
		static private var _state:String;
		static private var _chapterPath:String;
		static private var _rendersContainer:DisplayObjectContainer;
		static private var _getResolvePath:Function;
		
		public function Core()
		{
			throw(new Error("Core类是一个静态类"));
		}
		
		/**
		 * 章节路径;
		 */
		static public function getResolvePath(rp:String):String
		{
			return _getResolvePath(rp);
		}
		
		static public function set resolvePathHandle(value:Function):void
		{		
			_getResolvePath = value;
			addEventListener(CoreEvent.CORE_STATE_CHANGE, coreStateChangeHandle);
		}
		
		/**
		 * 课程状态;
		 */
		static public function get state():String
		{
			return _state;
		}
		
		static public function set state(value:String):void
		{
			if (value != _state)
			{
				_state = value;
				dispatchEvent(new CoreEvent(CoreEvent.CORE_STATE_CHANGE));
			}
		}
		
		/**
		 * 课程渲染器;
		 */
		static public function get rendersContainer():DisplayObjectContainer
		{
			return _rendersContainer;
		}
		
		static public function set rendersContainer(value:DisplayObjectContainer):void
		{
			_rendersContainer = value;
		}
		
		/**
		 * 获取显示对象;
		 * @param	displayObjectName
		 * @return
		 */
		static public function getDisplayObject(displayObjectName:String = null):DisplayObject
		{
			if (!_rendersContainer)
			{
				throw(new Error("Core类型渲染器为空"));
			}
			if (displayObjectName)
			{
				if (_rendersContainer[displayObjectName] is DisplayObject)
				{
					return _rendersContainer[displayObjectName];
				}
				else
				{
					var pathNameArray:Array = displayObjectName.split(".");
					var tempContainer:DisplayObjectContainer;
					var tempDisplayObject:DisplayObject = _rendersContainer;
					for each (var pathName:String in pathNameArray)
					{
						if (tempDisplayObject is DisplayObjectContainer)
						{
							tempContainer = tempDisplayObject as DisplayObjectContainer
						}
						else
						{
							throw(new Error(displayObjectName + "中," + pathName + "不是显示容器"));
						}
						if (tempContainer[pathName])
						{
							if (tempContainer[pathName] is DisplayObject)
							{
								tempDisplayObject = tempContainer[pathName] as DisplayObject;
							}
							else
							{
								throw(new Error(displayObjectName + "中," + pathName + "不是显示对象"));
							}
						}
					}
					return tempDisplayObject;
				}
			}
			return _rendersContainer;
		}
		
		/* DELEGATE flash.display.InteractiveObject */
		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			_rendersContainer.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		static public function dispatchEvent(event:Event):Boolean
		{
			return _rendersContainer.dispatchEvent(event);
		}
		
		static public function hasEventListener(type:String):Boolean
		{
			return _rendersContainer.hasEventListener(type);
		}
		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			_rendersContainer.removeEventListener(type, listener, useCapture);
		}
		
		static public function willTrigger(type:String):Boolean
		{
			return _rendersContainer.willTrigger(type);
		}
		
		/**
		 * TODO:完善Core初始化过程;
		 * @param	e
		 */
		static private function coreStateChangeHandle(e:CoreEvent):void
		{
			switch (state)
			{
				case CoreState.EMPTY: 
					state = CoreState.INIT_DATA_LOADING;
					DataInfo.$.loadURL("datas/init.xml");
					break;
				case CoreState.INIT_DATA_LOADED: 
					state = CoreState.PLAYERS_CREATING;
					PlayerPool.createPlayers(DataInfo.$.playerInfos);
					break;
				case CoreState.PLAYERS_CREATED: 
					ListPool.loadLists(DataInfo.$.listInfos, "datas/");
					break;
				case CoreState.LISTS_DATA_LOADED: 
					StreamPool.initStreams(DataInfo.$.streamInfos);
					break;
				case CoreState.STREAMS_READY:
					break;
				default: 
					break;
			}
		}
	}
}